package com.zhongxi.guoyuan.view.rewardCoin;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.TitleCateAdapter;
import com.zhongxi.guoyuan.adapter.rewardCoin.RewardHMAdapter;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.projectItem.ProjectItemData;
import com.zhongxi.guoyuan.fragment.hmfragment.AllMFragment;
import com.zhongxi.guoyuan.fragment.hmfragment.HMFragment;
import com.zhongxi.guoyuan.fragment.hmfragment.OneMFragment;
import com.zhongxi.guoyuan.fragment.hmfragment.TwoMFragment;
import com.zhongxi.guoyuan.widget.goldshop.NoScrollGridView;

import java.util.List;

/**
 * Created by Administrator on 2017/9/30.
 */

public class HMActivity extends BaseActivity {
    private int index = 0;
    private int currentIndex = 0;
    private LinearLayout ll_allCate;
    private List<ProjectItemData> titleList;
    private int position = 0;
    private TextView tv_cate_title;
    private RewardHMAdapter adapter;
    private TextView[] tabs;
    private Fragment[] fragments;
    private AllMFragment zeroFragment;
    private HMFragment firstFragment;
    private TwoMFragment secondFragment;
    private OneMFragment thirdFragment;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_hm);
        initTitle();
        tvTitle.setText("线上赚豆超市");
        Bundle bundle = getIntent().getExtras();
        titleList = (List<ProjectItemData>) bundle.getSerializable("category");
        position = bundle.getInt("position", 0);   //商品属性分类
        index = getIntent().getIntExtra("index", 0);   //红豆,美豆分类
        currentIndex = index;
        tv_cate_title = (TextView) findViewById(R.id.tv_cate_title);
        tv_cate_title.setText(titleList.get(position).getName());
        if (position > 0) {
            titleList.get(position).setSelect(true);
            titleList.get(0).setSelect(false);
        }
        ll_allCate = (LinearLayout) findViewById(R.id.ll_allCate);
    }

    @Override
    protected void initData() {
        super.initData();
        tabs = new TextView[4];
        tabs[0] = (TextView) findViewById(R.id.tv0);
        tabs[1] = (TextView) findViewById(R.id.tv1);
        tabs[2] = (TextView) findViewById(R.id.tv2);
        tabs[3] = (TextView) findViewById(R.id.tv3);
        zeroFragment = new AllMFragment();
        firstFragment = new HMFragment();
        secondFragment = new TwoMFragment();
        thirdFragment = new OneMFragment();
        fragments = new Fragment[]{zeroFragment, firstFragment, secondFragment, thirdFragment};
        tabs[index].setSelected(true);
        getSupportFragmentManager().beginTransaction().add(R.id.ll_container, fragments[index]).commit();
        getRefreshFragment();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ll_allCate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getPopupwindow(view);
            }
        });
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv0:
                index = 0;
                break;
            case R.id.tv1:
                index = 1;
                break;
            case R.id.tv2:
                index = 2;
                break;
            case R.id.tv3:
                index = 3;
                break;
        }
        refresh();
    }

    private void refresh() {
        if (index != currentIndex) {
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
            if (fragments[currentIndex].isAdded()) {
                trx.hide(fragments[currentIndex]);
            }
            if (!fragments[index].isAdded()) {
                trx.add(R.id.ll_container, fragments[index]);
            }
            trx.show(fragments[index]).commit();
            tabs[index].setSelected(true);
            tabs[currentIndex].setSelected(false);
            currentIndex = index;
        }
        getRefreshFragment();
    }

    private void getRefreshFragment() {
        switch (index) {
            case 0:
                zeroFragment.setPrimary(titleList.get(position).getId(),"");
                break;
            case 1:
                firstFragment.setPrimary(titleList.get(position).getId(), "");
                break;
            case 2:
                secondFragment.setPrimary(titleList.get(position).getId(), "");
                break;
            case 3:
                thirdFragment.setPrimary(titleList.get(position).getId(), "");
                break;
        }
    }

    private void getPopupwindow(View view) {
        View conView = View.inflate(context, R.layout.pop_reward_window, null);
        conView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        final PopupWindow popupWindow = new PopupWindow(conView, LinearLayout.LayoutParams.MATCH_PARENT, -2, true);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        int[] location = new int[2];
        view.getLocationInWindow(location);
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.popupwindow);
        conView.startAnimation(animation);
        popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, location[0], location[1] + view.getWidth() * 1 / 3);
        NoScrollGridView gv_category = (NoScrollGridView) conView.findViewById(R.id.gv_category);

        final TitleCateAdapter cateAdapter = new TitleCateAdapter(context, titleList);
        gv_category.setAdapter(cateAdapter);
        gv_category.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                position = i;
                String id = titleList.get(i).getId();
                switch (index) {
                    case 0:
                        zeroFragment.setPrimary(titleList.get(position).getId(), "");
                        break;
                    case 1:
                        firstFragment.setPrimary(id, "");
                        break;
                    case 2:
                        secondFragment.setPrimary(id, "");
                        break;
                    case 3:
                        thirdFragment.setPrimary(id, "");
                        break;
                }
                tv_cate_title.setText(titleList.get(i).getName());

                for (int j = 0; j < titleList.size(); j++) {
                    if (j == i) {
                        titleList.get(i).setSelect(true);
                    } else {
                        titleList.get(j).setSelect(false);
                    }
                }
                cateAdapter.notifyDataSetChanged();
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
    }
}
